/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.isi.pegasus.planner.subworkflow.estimator.implementation;

import edu.isi.pegasus.planner.classes.ADag;
import edu.isi.pegasus.planner.classes.PegasusBag;
import edu.isi.pegasus.planner.dax.ADAG;
import edu.isi.pegasus.planner.subworkflow.estimator.Estimator;
import edu.isi.pegasus.planner.subworkflow.partitioner.DAXAbstraction;
import edu.isi.pegasus.planner.subworkflow.partitioner.SiteAbstraction;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

/**
 *
 * @author wchen
 */
public class CPUTime extends Estimator{

        //maybe you should write this part to Estimator
    private ADag subDag;
    private double accumulatedCPUTime;

    public CPUTime(DAXAbstraction bigDax, SiteAbstraction site, ADag mDag, PegasusBag mBag, HashMap mJob2Time)
    {
        super(bigDax, site, mDag, mBag, mJob2Time);
        subDag = bigDax.subDag;
        long startTime = System.currentTimeMillis();
        accumulatedCPUTime = getAccumulatedCPUTime();
        long endTime = System.currentTimeMillis();
        System.out.println(bigDax.adag.getName() + ": "+ accumulatedCPUTime + " and takes " + (endTime-startTime) + " milliseconds");
        //accumulatedCPUTime = getAccumulatedCPUTimeParallel();

    }

    private double getAccumulatedCPUTime()
    {
        double runtime = 0.0;
        for(Iterator it = subDag.jobIterator(); it.hasNext();)
        {
            edu.isi.pegasus.planner.classes.Job job =
                    (edu.isi.pegasus.planner.classes.Job)it.next();
            //runtime += Double.parseDouble((String)job.vdsNS.get(Pegasus.RUNTIME_KEY));
            runtime += (Double)mJob2Time.get(job.jobID);
        }
        return runtime;
    }
    private double getAccumulatedCPUTimeParallel()
    {
        int num2slot = 8;
        double runtime = 0.0;
        ArrayList threads = new ArrayList();
        for(int i = 0; i < num2slot; i ++)
        {
            threads.add(new getCPUTime(subDag, i, mJob2Time));
        }
        for(int i = 0; i < num2slot; i ++)
        {
            getCPUTime gcp = (getCPUTime)threads.get(i);
            gcp.getDone();
            runtime += gcp.getRuntime();
        }
        return runtime;
    }
    public double getEstimatedRuntime(SiteAbstraction newsite)
    {

        ADAG dag = bigDax.adag;
        
        this.site = newsite;
        return accumulatedCPUTime / newsite.slot;

    }

}
